package cn.ydxlm.maplib.api;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import cn.ydxlm.maplib.entity.Location;

import java.util.ArrayList;
import java.util.List;

public abstract class OnPoiSearchResultListener implements PoiSearch.OnPoiSearchListener, GeocodeSearch.OnGeocodeSearchListener {
    private Location searchLocation;

    public Location getSearchLocation () {
        return searchLocation;
    }

    public void setSearchLocation (Location searchLocation) {
        this.searchLocation = searchLocation;
    }

    public abstract void onPoiResult (List<Location> data);

    @Override
    public void onPoiSearched (PoiResult poiResult, int i) {
        final List<Location> dataList = new ArrayList<>();
        if (poiResult == null) {
            onPoiResult(dataList);
            return;
        }
        final List<PoiItem> allPoi = poiResult.getPois();
        if (allPoi != null) {
            for (PoiItem info : allPoi) {
                final LatLonPoint latLng = info.getLatLonPoint();
                if (latLng == null || latLng.getLatitude() == 0 || latLng.getLongitude() == 0) {
                    continue;
                }
                final Location location = new Location(latLng.getLatitude(), latLng.getLongitude(), Location.SUCCESS, null);
                if (searchLocation != null) {
                    location.setProvince(searchLocation.getProvince());
                    location.setCity(searchLocation.getCity());
                    location.setArea(searchLocation.getArea());
                } else {
                    location.setProvince(info.getProvinceName());
                    location.setCity(info.getCityName());
                    location.setArea(info.getAdName());
                }
                location.setStreet(info.getSnippet());
                location.setName(info.getTitle());
                dataList.add(location);
            }
        }
        onPoiResult(dataList);
    }

    @Override
    public void onPoiItemSearched (PoiItem poiItem, int i) {

    }

    @Override
    public void onRegeocodeSearched (RegeocodeResult regeocodeResult, int rCode) {
        if (1000 == rCode) {
            final List<Location> dataList = new ArrayList<>();
            RegeocodeAddress address = regeocodeResult.getRegeocodeAddress();
            String area = address.getProvince();//省或直辖市
            String cityName = address.getCity();//地级市或直辖市
            String subLoc = address.getDistrict();//区或县或县级市
            List<PoiItem> pois = address.getPois();//获取周围兴趣点
            for (PoiItem info : pois) {
                final Location location = new Location(info.getLatLonPoint().getLatitude(),
                        info.getLatLonPoint().getLongitude(), Location.SUCCESS, null);
                location.setProvince(area);
                location.setCity(cityName);
                location.setArea(subLoc);
                location.setStreet(info.getSnippet());
                location.setName(info.getTitle());
                dataList.add(location);
            }
            onPoiResult(dataList);
        } else {
            onPoiResult(new ArrayList<Location>());
        }
    }

    @Override
    public void onGeocodeSearched (GeocodeResult geocodeResult, int i) {

    }
}
